package com.webside.trailerManagement.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.webside.base.basecontroller.BaseController;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.exception.AjaxException;
import com.webside.series.model.Bdictionary;
import com.webside.series.model.SeriesEntity;
import com.webside.series.service.BdictionaryService;
import com.webside.series.service.SeriesService;
import com.webside.trailerManagement.model.VideoFromWebEntity;
import com.webside.trailerManagement.service.VideoFromWebService;
import com.webside.util.Common;
import com.webside.util.PageUtil;
import com.webside.videoresource.model.VideoResourceEntity;
import com.webside.videoresource.service.VideoResourceService;

@Controller
@Scope("prototype")

@RequestMapping("/bvideoweb/")
public class VideoFromWebController extends BaseController {
	@Autowired
	private VideoFromWebService videoFromWebService;
	@Autowired
	private BdictionaryService bdictionaryService;
	@Autowired
	private VideoResourceService videoResourceService;
	@Autowired
	private SeriesService seriesService;
	@RequestMapping("listUI.html")
	public String listUI(Model model, HttpServletRequest request) {
		try {
			PageUtil page = new PageUtil();
			if (request.getParameterMap().containsKey("page")) {
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			model.addAttribute("page", page);

			return Common.BACKGROUND_PATH + "/videoFromWeb/list";
		} catch (Exception e) {
			throw new AjaxException(e);
		}
	}

	
	@RequestMapping("resources.html")
	public String resources(Model model,String ids) {
		List<SeriesEntity> list = seriesService.selectall(-1,null);
		int count=list.size();
		PageUtil pageUtil =new PageUtil();
		pageUtil.setPageNum(0);
		pageUtil.setPageSize(15);
		List<Bdictionary> channelList =  bdictionaryService.dictionaryList("CHANNEL", "");
		List<SeriesEntity> list1=seriesService.findListByName(pageUtil,"");
		for (int i = 0; i < list1.size(); i++) {
			for (int j = 0; j < channelList.size(); j++) {
				if(list1.get(i).getChannel().equals(channelList.get(j).getCode())){
					list1.get(i).setChannel(channelList.get(j).getName());
					 break;
				}
			}
		}
		model.addAttribute("videoResource_list", list1);
		int totalPageCount;
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		
		System.out.println("剧集数："+count);
		model.addAttribute("ids",ids);
		return Common.BACKGROUND_PATH + "/videoFromWeb/resources";
	}
	/*@RequestMapping("closeResources.html")
	public String closeResources(Model model,String ids) {
		PageUtil pageUtil1 = null;
		List<VideoFromWebEntity> list = videoFromWebService.findListByBindid(pageUtil1,Integer.parseInt(ids),"");
		model.addAttribute("ids", ids);
		int count=list.size();
		PageUtil pageUtil =new PageUtil();
		pageUtil.setPageNum(0);
		pageUtil.setPageSize(15);
		List<Bdictionary> channelList =  bdictionaryService.dictionaryList("CHANNEL", "");
		List<VideoFromWebEntity> list1=videoFromWebService.findListByBindid(pageUtil,Integer.parseInt(ids),"");
		for (int i = 0; i < list1.size(); i++) {
			for (int j = 0; j < channelList.size(); j++) {
				if(list1.get(i).getChannel().equals(channelList.get(j).getCode())){
					list1.get(i).setChannel(channelList.get(j).getName());
					 break;
				}
			}
		}
		model.addAttribute("videoResource_list", list1);
		int totalPageCount;
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		
		System.out.println("剧集数："+count);
		model.addAttribute("ids",ids);
		return Common.BACKGROUND_PATH + "/videoFromWeb/closeResources";
	}*/
	@RequestMapping(value="/searchPage.html", method={RequestMethod.GET,RequestMethod.POST})
	public String searchPage(Model model,SeriesEntity bSeries, String pSize,String pageNo,String sort,String name){
		PageUtil pageUtil =new PageUtil();
		int no=Integer.parseInt(pageNo)-1;
		pageUtil.setPageNum(no*Integer.parseInt(pSize));
		pageUtil.setPageSize(Integer.parseInt(pSize));
		System.out.println(sort);
		int totalPageCount;
		int count=seriesService.selectall(-1,sort).size();
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(sort==null){
			sort="";
		}
		List<Bdictionary> channelList =  bdictionaryService.dictionaryList("CHANNEL", "");
		List<SeriesEntity> list=seriesService.findListByName(pageUtil,sort);
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < channelList.size(); j++) {
				if(list.get(i).getChannel().equals(channelList.get(j).getCode())){
					list.get(i).setChannel(channelList.get(j).getName());
					 break;
				}
			}
		}
		model.addAttribute("videoResource_list", list);
		System.out.println("条数："+pageUtil.getPageNum()+"size:"+pageUtil.getPageSize());
		return Common.BACKGROUND_PATH+"/videoFromWeb/resources";
	}
	/*@RequestMapping(value="/searchUnbindidPage.html", method={RequestMethod.GET,RequestMethod.POST})
	public String searchUnbindidPage(Model model, String pSize,String pageNo,String sort,String ids){
		model.addAttribute("ids", ids);
		PageUtil pageUtil =new PageUtil();
		int no=Integer.parseInt(pageNo)-1;
		pageUtil.setPageNum(no*Integer.parseInt(pSize));
		pageUtil.setPageSize(Integer.parseInt(pSize));
		System.out.println(sort);
		int totalPageCount;
		int count=videoFromWebService.findListByBindid(pageUtil,Integer.parseInt(ids),sort).size();
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(sort==null){
			sort="";
		}
		List<Bdictionary> channelList =  bdictionaryService.dictionaryList("CHANNEL", "");
		List<VideoFromWebEntity> list=videoFromWebService.findListByBindid(pageUtil,Integer.parseInt(ids),sort);
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < channelList.size(); j++) {
				if(list.get(i).getChannel().equals(channelList.get(j).getCode())){
					list.get(i).setChannel(channelList.get(j).getName());
					 break;
				}
			}
		}
		model.addAttribute("videoResource_list", list);
		System.out.println("条数："+pageUtil.getPageNum()+"size:"+pageUtil.getPageSize());
		return Common.BACKGROUND_PATH+"/videoFromWeb/closeResources";
	}*/
	/**
	 * ajax分页动态加载模式
	 * 
	 * @param dtGridPager
	 *            Pager对象
	 * @throws Exception
	 */
	@RequestMapping(value = "/list.html", method = RequestMethod.POST)
	@ResponseBody
	public Object list(Model model, String gridPager, VideoFromWebEntity videoFromWebEntity,
			HttpServletResponse response) throws Exception {

		Map<String, Object> parameters = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(gridPager, Pager.class);
		// 判断是否包含自定义参数
		parameters = pager.getParameters();
		// 3、判断是否是导出操作
		if (pager.getIsExport()) {
			if (pager.getExportAllData()) {
				// 3.1、导出全部数据
				List<VideoFromWebEntity> list = videoFromWebService.queryListByPage(parameters);
				ExportUtils.exportAll(response, pager, list);
				return null;
			} else {
				// 3.2、导出当前页数据
				ExportUtils.export(response, pager);
				return null;
			}
		} else {

			// 设置分页，page里面包含了分页信息
			Page<Object> page = PageHelper.startPage(pager.getNowPage(), pager.getPageSize());
			List<VideoFromWebEntity> list = videoFromWebService.queryListByPage(parameters);

			parameters.clear();
			parameters.put("isSuccess", Boolean.TRUE);
			parameters.put("nowPage", pager.getNowPage());
			parameters.put("pageSize", pager.getPageSize());
			parameters.put("pageCount", page.getPages());
			parameters.put("recordCount", page.getTotal());
			parameters.put("startRecord", page.getStartRow());
			parameters.put("exhibitDatas", list);
			return parameters;
		}

	}

	@RequestMapping("editUI.html")
	public String editUI(Model model, HttpServletRequest request, Long id) {
		try {
			Bdictionary channel = new Bdictionary();
			// 查询清晰度
			List<Bdictionary> clarity = bdictionaryService.selectVideoByClarity(channel);
			model.addAttribute("clarity", clarity);
			// editUI查询
			VideoFromWebEntity videoFromWebEntity = videoFromWebService.findById(id);

			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));

			model.addAttribute("page", page);
			model.addAttribute("videoFromWebEntity", videoFromWebEntity);
			return Common.BACKGROUND_PATH + "/videoFromWeb/form";
		} catch (Exception e) {
			throw new AjaxException(e);
		}
	}

	// 修改
	@RequestMapping("edit.html")
	@ResponseBody
	public Object update(VideoFromWebEntity videoFromWebEntity, Model model) throws AjaxException {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			int result = videoFromWebService.update(videoFromWebEntity);
			System.out.println(result);
			if (result > 0) {

				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "编辑成功");

			} else {
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "编辑失败");
			}
		} catch (Exception e) {
			throw new AjaxException(e);
		}
		return map;
	}

	// 刪除
	@RequestMapping("deleteBatch.html")
	@ResponseBody
	public Object deleteBatch(String ids) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String[] seriesIds = ids.split(",");
			List<Long> list = new ArrayList<Long>();
			for (String string : seriesIds) {
				list.add(Long.valueOf(string));
			}
			int cnt = videoFromWebService.deleteBatchById(list);
			System.out.println(cnt);
			if (cnt == list.size()) {
				result.put("success", true);
				result.put("data", null);
				result.put("message", "删除成功");
			} else {
				result.put("success", false);
				result.put("data", null);
				result.put("message", "删除失败");
			}
		} catch (Exception e) {
			throw new AjaxException(e);
		}
		return result;
	}
	
	// 绑定剧集
	@RequestMapping("bindVideo.html")
	@ResponseBody
	public Object bindVideo(String ids,Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String[] seriesIds = ids.split(",");
			List<Integer> list = new ArrayList<Integer>();
			for (String string : seriesIds) {
				list.add(Integer.parseInt(string));
			}
			int cnt = videoFromWebService.bindVideo(id, list);
			if (cnt >0) {
				result.put("success", true);
				result.put("data", null);
				result.put("message", "绑定成功");
			} else {
				result.put("success", false);
				result.put("data", null);
				result.put("message", "绑定失败");
			}
		} catch (Exception e) {
			throw new AjaxException(e);
		}
		return result;
	}
	// 绑定剧集
		@RequestMapping("unbindVideo.html")
		@ResponseBody
		public Object unbindVideo(String ids) {
			Map<String, Object> result = new HashMap<String, Object>();
			try {
				int cnt = videoFromWebService.unbindVideo(Integer.parseInt(ids));
				if (cnt >0) {
					result.put("success", true);
					result.put("data", null);
					result.put("message", "解除绑定成功");
				} else {
					result.put("success", false);
					result.put("data", null);
					result.put("message", "解除绑定失败");
				}
			} catch (Exception e) {
				throw new AjaxException(e);
			}
			return result;
		}
	// 上线
	@RequestMapping("updateStatus.html")
	@ResponseBody
	public Object updateStatus(String ids) {
		System.out.println(ids);
		Map<String, Object> result = new HashMap<String, Object>();
		SeriesEntity entity = new SeriesEntity();
		try {
			String[] seriesIds = ids.split(",");
			List<Long> list = new ArrayList<Long>();
			for (String string : seriesIds) {
				list.add(Long.valueOf(string));
			}
			int cnt = videoFromWebService.updateOnline(list);
			if (cnt == list.size()) {
				result.put("success", true);
				result.put("data", null);
				result.put("message", "上线成功");
			} else {
				result.put("success", false);
				result.put("data", null);
				result.put("message", "上线失败");
			}
		} catch (Exception e) {
			throw new AjaxException(e);
		}
		return result;
	}

	// 下线
	@RequestMapping("updateStatus1.html")
	@ResponseBody
	public Object updateStatus1(String ids) {
		System.out.println(ids);
		Map<String, Object> result = new HashMap<String, Object>();
		SeriesEntity entity = new SeriesEntity();
		try {
			String[] seriesIds = ids.split(",");
			List<Long> list = new ArrayList<Long>();
			for (String string : seriesIds) {
				list.add(Long.valueOf(string));
			}
			int cnt = videoFromWebService.updateOffline(list);
			if (cnt == list.size()) {
				result.put("success", true);
				result.put("data", null);
				result.put("message", "下线成功");
			} else {
				result.put("success", false);
				result.put("data", null);
				result.put("message", "下线失败");
			}
		} catch (Exception e) {
			throw new AjaxException(e);
		}
		return result;
	}

}
